package com.qf.zyh.services.admin.Impl;

import com.qf.zyh.common.Const;
import com.qf.zyh.dao.admin.AdminDao;
import com.qf.zyh.dao.admin.Impl.AdminDaoImpl;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.admin.AdminSer;
import com.qf.zyh.util.DBUtil;
import com.qf.zyh.util.MD5Utils;

import java.util.List;

public class AdminSerImpl implements AdminSer {
    AdminDao ad = new AdminDaoImpl();

    @Override
    public User adminLogin(String username, String password) {
         User user =ad.getadmin(username);
        if (user!=null) {
            if (user.getUpassword().equals(MD5Utils.md5(password))&&user.getUrole()==Const.UserCont.IS_ADMIN) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> seleall() {
        try {
            DBUtil.begin();
            List<User> list = ad.seleall();
            DBUtil.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    @Override
    public List<User> selein(String username, String sex) {
        try {
            DBUtil.begin();
            List<User> list = ad.selein(username,sex);
            DBUtil.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    @Override
    public Boolean deleUser(Integer uid) {
        try {
            DBUtil.begin();
            int a = ad.deleUser(uid);
            DBUtil.commit();
            if (a>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }
}
